package zsc.nine.onlinemall.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zsc.nine.onlinemall.entity.Product;
import zsc.nine.onlinemall.service.ProductService;

import java.util.List;

/**
 * @ClassName: ProductController
 * @Description: Controller of Product operation
 * @Author: Zainus
 * @CreateDate: Jul.23, 2021
 * @UpdateDate: Jul.23, 2021
 * @Version: [v1.0]
 */

/**
 * @类名: ProductController
 * @描述: 产品管理控制类接口
 * @创建人: 叶增钥
 * @创建时间: 2021年7月23日
 * @更新时间: 2021年7月23日
 * @版本: [v1.0]
 */
@RequestMapping("/Product")
@RestController
public class ProductController {

    @Autowired
    ProductService productService;

    /**
     *
     * @param ProductName
     * @param Image
     * @param OriginPrice
     * @param NowPrice
     * @param Stock
     * @return
     */
    @PostMapping("/Create")
    public String ProductCreate(@RequestParam(value = "ProductName", required = false) String ProductName,
                                @RequestParam(value = "Image", required = false) String Image,
                                @RequestParam(value = "OriginPrice", required = false) String OriginPrice,
                                @RequestParam(value = "NowPrice", required = false) String NowPrice,
                                @RequestParam(value = "Stock", required = false) String Stock) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ProductCreateResult", "Fail");

        if (ProductName == null
                || Image == null
                || OriginPrice == null
                || NowPrice == null
                || Stock == null) {
            jsonObject.put("ProductCreateResult", "Parameter-Null");
        } else if (ProductName.trim().isEmpty()
                || Image.trim().isEmpty()
                || OriginPrice.trim().isEmpty()
                || NowPrice.trim().isEmpty()
                || Stock.trim().isEmpty()) {

            jsonObject.put("ProductCreateResult", "Empty");

        } else {

            if (productService.createProduct(ProductName, Image, OriginPrice, NowPrice, Stock)) {
                jsonObject.put("ProductCreateResult", "Success");
            }

        }


        return jsonObject.toJSONString();

    }

    /**
     *
     * @param ProductId
     * @return
     */
    @PostMapping("/Delete")
    public String ProductDelete(@RequestParam(value = "ProductId", required = false) String ProductId) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ProductDeleteResult", "Fail");

        if (ProductId == null) {
            jsonObject.put("ProductDeleteResult", "Parameter-Null");
        } else if (ProductId.trim().isEmpty()) {
            jsonObject.put("ProductDeleteResult", "Empty");
        } else if (!productService.productExist(ProductId)) {
            jsonObject.put("ProductDeleteResult", "Wrong-Id");
        } else if (productService.deleteProduct(ProductId)) {
            jsonObject.put("ProductDeleteResult", "Success");
        }


        return jsonObject.toJSONString();

    }

    /**
     *
     * @return
     */
    @PostMapping("/Update")
    public String ProductUpdate() {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ProductUpdateResult", "Fail");

        return jsonObject.toJSONString();

    }

    /**
     * @param ProductId 产品Id
     * @return 一用JSONString的ProductGetResult字段携带，Parameter-Null为传入参数缺失，Empty为后端判断数据为空，
     *  Wrong-Id为该产品不存在，Fail为失败，Success为成功
     *  二携带产品对象用JSONString的ProductGetObjectResult字段携带
     */
    @PostMapping("/Get")
    public String ProductGet(@RequestParam(value = "ProductId", required = false) String ProductId) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ProductGetResult", "Fail");

        if (ProductId == null) {
            jsonObject.put("ProductGetResult", "Parameter-Null");
        } else if (ProductId.trim().isEmpty()) {
            jsonObject.put("ProductGetResult", "Empty");
        } else if (!productService.productExist(ProductId)) {
            jsonObject.put("ProductGetResult", "Wrong-Id");
        } else {
            Product product = productService.findProductById(ProductId);
            if (product != null) {
                jsonObject.put("ProductGetResult", "Success");
                jsonObject.put("ProductGetObjectResult", product);
            }
        }

        return jsonObject.toJSONString();

    }

    @PostMapping("/OnSale")
    public String ProductOnSale(@RequestParam(value = "ProductId") String ProductId) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ProductOnSaleResult", "Fail");

        if (ProductId == null) {
            jsonObject.put("ProductOnSaleResult", "Parameter-Null");
        } else if (ProductId.trim().isEmpty()) {
            jsonObject.put("ProductOnSaleResult", "Empty");
        } else if (!productService.productExist(ProductId)) {
            jsonObject.put("ProductOnSaleResult", "Wrong-Id");
        } else if (productService.productOnSale(ProductId)) {
            jsonObject.put("ProductOnSaleResult", "Success");
        }


        return jsonObject.toJSONString();
    }

    /**
     *
     * @param ProductId
     * @return
     */
    @PostMapping("/OffSale")
    public String ProductOffSale(@RequestParam(value = "ProductId") String ProductId) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ProductOffSaleResult", "Fail");

        if (ProductId == null) {
            jsonObject.put("ProductOffSaleResult", "Parameter-Null");
        } else if (ProductId.trim().isEmpty()) {
            jsonObject.put("ProductOffSaleResult", "Empty");
        } else if (!productService.productExist(ProductId)) {
            jsonObject.put("ProductOffSaleResult", "Wrong-Id");
        } else if (productService.productOffSale(ProductId)) {
            jsonObject.put("ProductOffSaleResult", "Success");
        }


        return jsonObject.toJSONString();
    }

    /**
     * @return 一用JSONString的ProductGetListResult字段携带，Parameter-Null为传入参数缺失，Empty为后端判断数据为空，
     *  Fail为失败，Success为成功
     *  二携带产品对象列表用JSONString的ProductGetListObjectResult字段携带
     */
    @PostMapping("/GetList")
    public String ProductGetList() {

        JSONObject jsonObject = new JSONObject();


        jsonObject.put("ProductGetListObjectResult", productService.getAll());

        return jsonObject.toJSONString();

    }

    /**
     * @return 用JSONString的ProductGetListSizeResult字段携带
     */
    @PostMapping("/GetListSize")
    public String ProductGetListSize() {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ProductGetListSizeResult", productService.getLength());

        return jsonObject.toJSONString();

    }

    @PostMapping("/Find")
    public String ProductFind(@RequestParam(value = "LikeName", required = false) String LikeName) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ProductFindResult", "Fail");

        if (LikeName == null) {
            jsonObject.put("ProductFindResult", "Parameter-Null");
        } else if (LikeName.trim().isEmpty()) {
            jsonObject.put("ProductFindResult", "Empty");
        } else {
            List<Product> productsList = productService.findByLikeName(LikeName);
            if (productsList.size() != 0) {
                jsonObject.put("ProductFindResult", "Success");
                jsonObject.put("ProductFindObjectResult", productsList);
            }
        }


        return jsonObject.toJSONString();
    }


}
